package com.springboot.middleware.middleware_grab_red_envelopes.server.service;

import com.springboot.middleware.middleware_grab_red_envelopes.model.entity.RedDetail;
import com.springboot.middleware.middleware_grab_red_envelopes.model.entity.RedRecord;
import com.springboot.middleware.middleware_grab_red_envelopes.model.entity.RedRobRecord;
import com.springboot.middleware.middleware_grab_red_envelopes.model.mapper.RedDetailMapper;
import com.springboot.middleware.middleware_grab_red_envelopes.model.mapper.RedRecordMapper;
import com.springboot.middleware.middleware_grab_red_envelopes.model.mapper.RedRobRecordMapper;
import com.springboot.middleware.middleware_grab_red_envelopes.server.dto.RedPacketDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@EnableAsync
public class RedServiceImpl implements RedService {

    private static final Logger log = LoggerFactory.getLogger(RedServiceImpl.class);

    //发红包时红包全局唯一标识串等信息接口
    @Autowired
    private RedRecordMapper redRecordMapper;

    //发红包时随机数算法生成的随机金额列表等接口
    @Autowired
    private RedDetailMapper redDetailMapper;

    //抢红包时相关数据信息操作接口
    @Autowired
    private RedRobRecordMapper redRobRecordMapper;

    /**
     * 记录发红包时红包的唯一标识码,随机金额列表和个数等信息
     * 发红包记录-异步方式
     *
     * @param dto   红包dto,存在用户Id,红包个数和总金额信息
     * @param redId 存在红包的唯一标识码
     * @param list  随机金额列表
     * @throws Exception
     */
    @Override
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void recordRedPacket(RedPacketDto dto, String redId, List<Integer> list) throws Exception {
        RedRecord redRecord = new RedRecord();

        redRecord.setUserId(dto.getUserId());
        redRecord.setRedPacket(redId);
        redRecord.setTotal(dto.getTotal());
        redRecord.setAmount(BigDecimal.valueOf(dto.getAmount()));
        redRecord.setCreateTime(new Date());
        //将对象信息插入数据库
        redRecordMapper.insertSelective(redRecord);

        //定义红包随机金额明细实体类对象
        RedDetail redDetail;
        RedRecord tmp = redRecordMapper.selectIdByUserIdAndRedPacket(redRecord.getUserId(), redRecord.getRedPacket());
        for (Integer i : list) {
            redDetail = new RedDetail();
            redDetail.setRecordId(tmp.getId());
            redDetail.setAmount(BigDecimal.valueOf(i));
            redDetail.setCreateTime(new Date());
            redDetailMapper.insertSelective(redDetail);
        }
    }

    /**
     * 记录抢红包时用户强到的红包金额等信息
     *
     * @param userId 用户id
     * @param redId  红包id
     * @param amount 红包金额
     * @throws Exception
     */
    @Override
    @Async
    public void recordRobRedPacket(Integer userId, String redId, BigDecimal amount) throws Exception {
        RedRobRecord redRobRecord = new RedRobRecord();
        redRobRecord.setUserId(userId);
        redRobRecord.setRedPacket(redId);
        redRobRecord.setAmount(amount);
        redRobRecord.setRobTime(new Date());
        try {
            redRobRecordMapper.insertSelective(redRobRecord);
        } catch (Exception e) {
            log.error(e.toString());
        }
    }
}
